﻿using MediatR;
using RedDragon.Common.Domain.AuthorizeRBAC;
using RedDragon.Common.Dtos;
using RedDragon.Common.Infrastructure.Interface;
using RedDragon.RBAC.ErrorCodeEnum;
using SqlSugar;
using System.DirectoryServices.Protocols;
using T.Common.Domain.Common;

namespace RedDragon.RBAC.API.Application.Common.Permission
{
    public class GetPermissionTypesHanlder : IRequestHandler<GetPermissionTypesCommand, Result>
    {
        private readonly ISqlSugarClient db;
        /// <summary>
        /// 权限分类仓储
        /// </summary>
        private readonly IBaseRepository<PermissionTypes> permissionTypesRepository;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<GetPermissionTypesHanlder> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="permissionTypesRepository"></param>
        /// <param name="logger"></param>
        public GetPermissionTypesHanlder(ISqlSugarClient db,IBaseRepository<PermissionTypes> permissionTypesRepository, ILogger<GetPermissionTypesHanlder> logger)
        {
            this.db = db;
            this.permissionTypesRepository = permissionTypesRepository;
            this.logger = logger;
        }
        /// <summary>
        /// 权限分类获取
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result> Handle(GetPermissionTypesCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Search };
            try
            {
                var list = await (await permissionTypesRepository.GetAll()).ToListAsync();
                if (list != null)
                {
                    result.Data = list;
                    result.Code = (int)CommonErrorCode.Success;
                    result.Msg = "权限分类获取成功";
                }
            }
            catch (Exception ex)
            {
                logger.LogError("权限分类获取异常");
                result.Msg = "权限分类获取异常";
            }
            return result;
        }
    }
}
